<?php
class ContactsController extends AppController {

	var $name = 'Contacts';
    var $components = array('Json');
    var $helpers = array('Javascript');

    function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->allow('index','add');
    }

    /**
     * Receiving incoming contact
     * usage in : posts/index.ctp and contact/index.ctp
     */
    function add(){
        $this->layout = 'ajax';
        Configure::write('debug',0);
        App::import('Sanitize');

        /* Defining tags array variables from ajax input */
        $name = Sanitize::escape($this->params['form']['name']);
        $email = Sanitize::escape($this->params['form']['email']);
        $website = Sanitize::escape($this->params['form']['website']);
        $description = Sanitize::html($this->params['form']['description']);
        $ip = Sanitize::escape($this->params['form']['user']);

        if(empty($name) || empty($email) || empty($ip) || empty($description)){
            $this->Json->error('0');
        }else{
            $check = $this->Contact->checkDuplicateContent($email,$description);
            if(!empty($check)){
                $this->Json->error('1');
            }else{
                $this->Contact->create();
                $this->data['Contact']['name']=$name;
                $this->data['Contact']['email']=$email;
                $this->data['Contact']['website']=$website;
                $this->data['Contact']['description']=$description;
                $this->data['Contact']['ip']=$ip;
                $this->data['Contact']['created']=date('y-m-d H:i:s');
                $this->Contact->set($this->data);
                if($this->Contact->validates()){
                    if($this->Contact->save($this->data)){
                        $this->Json->result('1');
                    }else{
                        $this->Json->error('2');
                    }
                }else{
                    $error = $this->Contact->invalidFields();
                    $this->Json->error($error);
                }
            }
        }

    }

    /**
     * Contact page
     */
    function index(){

    }

}
?>